﻿using CustomCharacter;
using CustomCharacter.Ability;
using Exiled.API.Enums;
using Exiled.Events.EventArgs.Interfaces;
using InventorySystem;
using InventorySystem.Items.Firearms;
using InventorySystem.Items.Firearms.Attachments;
using MEC;
using PlayerRoles;
using PlayerStatsSystem;
using Respawning;
using RgCore.Events;
using RgCore.GameData;
using System.Collections.Generic;
using System.Linq;
using CustomCharacter.Interfaces;
using RgCore.CustomSide;
using UnityEngine;
using VoiceChat;
using Player = Exiled.API.Features.Player;

namespace Scp550
{
    [RoleInfo("SCP-550", Side.Scp, PlayerRoles.RoleTypeId.NtfPrivate, PlayerRoles.RoleTypeId.Tutorial,
        MaxHealth = 300)]
    public class Scp550 : CustomCharacter.CustomCharacter, IScpNumber, ICustomSpawnpoint, ITeamSpawn, IVoiceChannelModifcation
    {
        public Scp550(int id) : base(id)
        {
        }

        public override Color32 RoleColor => new Color32(255, 69, 0, 255);

        public string ScpNumber => "550";

        public Vector3 SpawnPosition => Exiled.API.Extensions.RoleExtensions.GetRandomSpawnLocation(PlayerRoles.RoleTypeId.Scp939).Position;

        public SpawnableTeamType SpawnType => SpawnableTeamType.NineTailedFox;

        public byte MaxWaves => 1;
        public VoiceChatChannel[] AvailableVoiceChannels => new VoiceChatChannel[] { VoiceChatChannel.ScpChat };

        public bool PrimaryModification => true;

        public override IEnumerable<Ability> AbilityRegister()
        {
            return new Ability[]
            {
                new AttributeAbility("Scp550_Attr_1",
    "在自己10m范围内有玩家死亡时，会为自己回复最大生命10%的血量，如果由自己击杀该玩家，则额外增加2%最大血量上限的恢复量。",
    Attribute_1, false, new IExiledEvent[] { EventProcessor.ReadyDying }, null, GetType())
            };
        }

        public override void Role_Activate()
        {
            base.Role_Activate();
            Timing.CallDelayed(1f, () =>
            {
                Player.ClearInventory();
                Player.Scale = new Vector3(0.5f, 1f, 0.5f);
                foreach (Player hub2 in Player.List)
                {
                    if (Player.IsFriendly(hub2) && hub2.Role != RoleTypeId.Scp079)
                    {
                        Player.Position = hub2.Position;
                        break;
                    }
                }
                Player.Inventory.ServerAddItem(ItemType.SCP500);
                Player.Inventory.ServerAddAmmo(ItemType.Ammo9x19, 100);
                Firearm firearm = (Firearm)Player.Inventory.ServerAddItem(ItemType.GunCrossvec);
                if (AttachmentsServerHandler.PlayerPreferences.TryGetValue(Player.ReferenceHub, out Dictionary<ItemType, uint> dictionary) && dictionary.TryGetValue(firearm.ItemTypeId, out uint code))
                {
                    firearm.ApplyAttachmentsCode(code, true);
                }
                FirearmStatusFlags firearmStatusFlags = FirearmStatusFlags.MagazineInserted;
                if (firearm.HasAdvantageFlag(AttachmentDescriptiveAdvantages.Flashlight))
                {
                    firearmStatusFlags |= FirearmStatusFlags.FlashlightEnabled;
                }
                firearm.Status = new FirearmStatus(firearm.AmmoManagerModule.MaxAmmo, firearmStatusFlags, firearm.GetCurrentAttachmentsCode());
            });
        }

        public override void Role_Deactive()
        {
            base.Role_Deactive();
            Timing.CallDelayed(1f, delegate ()
            {
                Player.Scale = Vector3.one * 1f;
            });
        }

        public static AbilityOutput Attribute_1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            if (trigger != AbilityTrigger.Event)
                return false;
            if (e is DamageResultingEventArgs ev && ev.DamageHandler is AttackerDamageHandler)
            {
                float percent = Vector3.Distance(ev.Player.Position, player.Position) <= 10f ? 0.1f : 0f;
                if (ev.Attacker == player)
                    percent += percent > 0 ? 0.02f : 0.05f;
                if (percent > 0)
                    player.Heal(player.MaxHealth * percent);
                return true;
            }
            return false;
        }

        public void OnTeamSpawn(List<Player> allPlayers)
        {
            List<Player> ava = allPlayers.Where(x => x.Role.Type == Attribute.OriginRole && !x.IsCustomCharacter() &&
GameplayData.GameDatas.FirstOrDefault(y => y.Player == x).KeyBinded).ToList();
            if (ava.Count > 0)
            {
                Player tar = ava[UnityEngine.Random.Range(0, ava.Count)];
                SpawnPlayer(tar, GetType());
            }
        }
    }
}